const $api = window.$api;
import { useDownloadFile } from '@/hooks';

export const getLinkData = (data) => {
  const originStr = data.filedType === 'HYPERLINK' ? data.displayValue : data.descriptionFile;
  const str = JSON.parse(originStr);
  const arr = Object.keys(str);
  const regex = /(<a>)([^]+)(<\/a>)/g;
  const splitStr = [];
  arr.forEach((item) => {
    splitStr.push(str[item]);
  });
  const newArr = arr.map((item) => {
    const itemStr = str[item].replace(regex, ',,,');
    const itemStr_n = str[item].split('\n');
    return {
      fileOid: item,
      descPrev: itemStr.split(',,,')[0] || itemStr_n[0].replace(regex, ''),
      descNext: itemStr_n[1] || itemStr.split(',,,')[1],
      fileName:
        data.filedType === 'HYPERLINK'
          ? str[item]
          : str[item]?.match(regex)[0].replace(regex, '$2'),
    };
  });
  return newArr;
};
export const downloadTips = ({ fileOid, fileName }) => {
  $api.createApi
    .downloadContent({ oid: fileOid })
    .then((result) => {
      if (!result) return window.$message.error('下载失败');
      useDownloadFile(result, fileName);
    })
    .catch((err) => {});
};
